
console.log('content.js has been injected!');


if (document.URL.includes('talbots.com')) {
  const destDiv = document.querySelectorAll('.slick-track')[1];
  // destDiv下的li元素下的a(href中不能包含 video)下的img
  const imgs = destDiv.querySelectorAll('li a:not([href*="video"]) img');
  let imgUrls = [];
  for (let img of imgs) {
    let imgFirstUrl = img.src.split('?')[0];
    let imgUrl = imgFirstUrl + '?sfrm=jpg';
    imgUrls.push(imgUrl);
  }
  // 通知background.js下载图片
  chrome.runtime.sendMessage({action: 'downloadImages', urls: imgUrls});
} else if (document.URL.includes('chicos.com/store/product')) {
  const destDiv = document.querySelector('.product-alt-images');
  // destDiv下的li元素下的a(href中不能包含 video)下的img
  const imgs = destDiv.querySelectorAll('li a:not([href*="video"]) img');
  let imgUrls = [];
  for (let img of imgs) {
    let imgFirstUrl = img.src.split('?')[0];
    let imgUrl = imgFirstUrl.replace('.jpg', '_large.jpg')
    imgUrls.push(imgUrl);
  }
  // 通知background.js下载图片
  chrome.runtime.sendMessage({action: 'downloadImages', urls: imgUrls});
} else if (document.URL.includes('torrid.com/product')) {
  const destDiv = document.querySelector('.video-container')
  const imgs = destDiv.querySelectorAll('img');
  let imgUrls = [];
  for (let img of imgs) {
    imgUrls.push(img.src);
  }
  // 通知background.js下载图片
  chrome.runtime.sendMessage({action: 'downloadImages', urls: imgUrls});
}

else if (document.URL.includes('titlenine.com/p')) {
  const destDiv = document.querySelectorAll('.slick-track')[1];
  const imgs = destDiv.querySelectorAll('img');
  let imgUrls = [];
  for (let img of imgs) {
    imgUrls.push(img.src.replace('sw=800&sh=800', 'sw=2000&sh=2000'));
  }
  // 通知background.js下载图片
  chrome.runtime.sendMessage({action: 'downloadImages', urls: imgUrls});
} else if (document.URL.includes('tobi.com/product')) {
  // 取出这种div，这种div的class中包含desktop-thumbs，并且直接上级div的class中不包含hide

  // 获取所有包含 'desktop-thumbs' 的 div
  let divs = document.querySelectorAll('div.desktop-thumbs');
  // 遍历所有这些 div 元素
  divs.forEach(div => {
    // 获取当前 div 的父元素
    let parentDiv = div.parentElement;

    // 检查父元素的 class 是否包含 'hide'
    if (!parentDiv.classList.contains('hide')) {
      // 如果父元素不包含 'hide'，则处理这个 div
      const imgElements = div.querySelectorAll('img');
      let imgUrls = [];
      imgElements.forEach(img => {
        imgUrls.push(img.src.replace('/xs/', '/lg/'));
      })
      // 通知background.js下载图片
      chrome.runtime.sendMessage({action: 'downloadImages', urls: imgUrls});
    }
  });

} else if (document.URL.includes('shopcider.com')) {
  alert('没有自动下载图片？')
} else if (document.URL.includes('venus.com/products')) {
  const liElements =
      document.querySelector('.gallery-thumbs').querySelector('ul').children;

  let imageUrls = [];
  for (let liElement of liElements) {
    const src = liElement.querySelector('button').querySelector('img').src;
    const imgSrc = src.split('.jpg')[0] + '.jpg';
    imageUrls.push(imgSrc);
  }
  // 通知background.js下载图片
  chrome.runtime.sendMessage({action: 'downloadImages', urls: imageUrls});
} else if (document.URL.includes('petallush.com/')) {
  const destDiv = document.querySelectorAll('.lof-navigator')[0];
  const lis = destDiv.querySelectorAll('li');
  let imgUrls = [];
  for (let li of lis) {
    let img = li.querySelector('img');
    imgUrls.push(img.src.replace('/thumbnail/108x162/', '/image/600x900/'));
  }
  // 通知background.js下载图片
  chrome.runtime.sendMessage({action: 'downloadImages', urls: imgUrls});
} else if (document.URL.includes('.curvedream.com/')) {
  const destDiv = document.querySelectorAll('.lof-navigator')[0];
  const lis = destDiv.querySelectorAll('li');
  let imgUrls = [];
  for (let li of lis) {
    let img = li.querySelector('img');
    imgUrls.push(img.src.replace('/thumbnail/108x162/', '/image/600x900/'));
  }
  // 通知background.js下载图片
  chrome.runtime.sendMessage({action: 'downloadImages', urls: imgUrls});
} else if (document.URL.includes('jjshouse.com')) {
  
  // 取出当前选中的是all 还是48小时
  const spans = document.querySelectorAll('.styles-show-select-types span');
  const selectedIndex =
      Array.from(spans).findIndex(span => span.classList.contains('selected'));
  // 当前应该在哪个div下取颜色
  const colorDiv = document.querySelectorAll('.pis-style-box')[selectedIndex];
  const dls = colorDiv.querySelectorAll('a dl');
  let selectedColorName = '';
  for (let dl of dls) {
    if (dl.querySelector('dt').classList.contains('selected')) {
      selectedColorName = dl.getAttribute('data-value');
      break;
    }
  }
  if (selectedColorName.length > 0) {
 
    // 以下是我获取的所有的图片
    let imgs = document.querySelector('.carousel').querySelectorAll('img')
    // 现在需要过滤出来这个颜色的图片，过滤规则是图片上有对象颜色的名字，color="Orange"
    let imgUrls = [];

    for (let img of imgs) {
      let imgColor = img.getAttribute('color');
      if (imgColor === selectedColorName) {
        let tempSrc = img.getAttribute('data-magnify');
        imgUrls.push(tempSrc);
      }
    }
    if (imgUrls.length > 0) {
      console.log('开始下载图片' + imgUrls.length + '张');
      console.log(imgUrls);
      chrome.runtime.sendMessage({action: 'downloadImages', urls: imgUrls});
    } else {
      console.log('没有找到对应颜色的图片');
    }
  } else {
    console.log('没有选中颜色');
  }
}